package com.zl.learning.xml.test;

import org.w3c.dom.*;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;

public class ParseXml {
    public static class ParseXml2 {
        public static void main(String[] args){
            DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
            DocumentBuilder builder;
            try {
                builder = factory.newDocumentBuilder();
                Document document=builder.parse("电影院.xml");
                NodeList movies=document.getElementsByTagName("Movie");

                for(int i=0;i<movies.getLength();i++){
                    Element elt = (Element)movies.item(i);
                    Node name=elt.getElementsByTagName("name").item(0);
                    Node Poster=elt.getElementsByTagName("Poster").item(0);
                    Node Director=elt.getElementsByTagName("Director").item(0);
                    Node Actor=elt.getElementsByTagName("Actor").item(0);
                    Node Type=elt.getElementsByTagName("Type").item(0);
                    Node Price=elt.getElementsByTagName("Price").item(0);
                    System.out.println("电影名称是"+name.getFirstChild().getNodeValue());
                    System.out.println("电影英文名称是"+Poster.getFirstChild().getNodeValue());
                    System.out.println("电影导演是"+Director.getFirstChild().getNodeValue());
                    System.out.println("电影演员是"+Actor.getFirstChild().getNodeValue());
                    System.out.println("电影类型是"+Type.getFirstChild().getNodeValue());
                    System.out.println("电影价格是"+Price.getFirstChild().getNodeValue());
                    //Node node = movies.item(i);
                    Node fatherNode = movies.item(0);
                    NamedNodeMap attributes = fatherNode.getAttributes();
                    for (int j = 0; j < attributes.getLength(); j++) {
                        Node attribute = attributes.item(j);
                        System.out.println( attribute.getNodeName()+"," + attribute.getNodeValue());
                    }
                    //System.out.println(node.getNodeName()+","+node.getNodeValue());
                    /*System.out.println(movies.item(i).getNodeName().equals("Movie"));
                    if(node.getNodeType()==Node.ELEMENT_NODE){
                        if(node.getNodeName().equals("name")){
                            System.out.println("电影名称是"+node.getFirstChild().getNodeValue());
                        }else if(node.getNodeName().equals("Poster")){
                            System.out.println("电影英文名称是"+node.getFirstChild().getNodeValue());
                        }else if(node.getNodeName().equals("Director")){
                            System.out.println("电影导演是"+node.getFirstChild().getNodeValue());
                        }else if(node.getNodeName().equals("Actor")){
                            System.out.println("电影演员是"+node.getFirstChild().getNodeValue());
                        }else if(node.getNodeName().equals("Romance")){
                            System.out.println("电影类型是"+node.getFirstChild().getNodeValue());
                        }else if(node.getNodeName().equals("Price")){
                            System.out.println("电影价格是"+node.getFirstChild().getNodeValue());
                        }else if(node.getNodeName().equals("Schedule")){
                            if(node.getNodeName().equals("Item")){
                            System.out.println("电影时间是"+node.getFirstChild().getNodeValue());
                            }else{
                            System.out.println("电影时间是"+node.getFirstChild().getNodeValue());}
                        }
                    }*/
                }
            } catch (ParserConfigurationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                } catch (SAXException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    public static class ParseXml3 {
        public static void main(String[] args) {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder;
            try {
                builder = factory.newDocumentBuilder();
                Document document = builder.parse("电影院.xml");
                NodeList movies = document.getElementsByTagName("Movie");

                Node fatherNode = movies.item(0);
                NodeList childNodes = fatherNode.getChildNodes();
                for (int j = 0; j < childNodes.getLength(); j++) {
                    Node childNode = childNodes.item(j);
                    if (childNode instanceof Element) {
                        if (childNode.getNodeName().equals("name")) {
                            System.out.println("电影名称是" + childNode.getFirstChild().getNodeValue());
                        } else if (childNode.getNodeName().equals("Poster")) {
                            System.out.println("电影英文名称是" + childNode.getFirstChild().getNodeValue());
                        } else if (childNode.getNodeName().equals("Director")) {
                            System.out.println("电影导演是" + childNode.getFirstChild().getNodeValue());
                        } else if (childNode.getNodeName().equals("Actor")) {
                            System.out.println("电影演员是" + childNode.getFirstChild().getNodeValue());
                        } else if (childNode.getNodeName().equals("Romance")) {
                            System.out.println("电影类型是" + childNode.getFirstChild().getNodeValue());
                        } else if (childNode.getNodeName().equals("Price")) {
                            System.out.println("电影价格是" + childNode.getFirstChild().getNodeValue());
                        } else if (childNode.getNodeName().equals("Schedule")) {
                            NodeList items = childNode.getChildNodes();
                            for (int i = 0; i < items.getLength(); i++) {
                                Node child = items.item(i);
                                if (childNode instanceof Element) {
                                    if (child.getNodeName().equals("Item")) {
                                        System.out.println("电影时间是" + child.getFirstChild().getNodeValue());
                                    }
                                }
                            }
                        }

                    }
                }
            } catch (ParserConfigurationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SAXException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    public static class ParseXml4 {
        public static void main(String[] args) {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder;
            try {
                builder = factory.newDocumentBuilder();
                Document document = builder.parse("电影院.xml");
                NodeList movies = document.getElementsByTagName("Movie");
                String movieName = "";
                String movieActor = "";
                String moviePoster = "";
                String movieDirector = "";
                String movieRomance = "";
                String moviePrice = "";
                String movieType = "";
                String item = "";
                for (int i = 0; i < movies.getLength(); i++) {
                    Node no = movies.item(i);
                    NodeList list = no.getChildNodes();
                    for (int y = 0; y < list.getLength(); y++) {
                        Node node = list.item(y);
                        if (node.getNodeType() == Node.ELEMENT_NODE) {
                            if (node.getNodeName().equals("name")) {
                                movieName = node.getFirstChild().getNodeValue();
                            } else if (node.getNodeName().equals("Poster")) {
                                moviePoster = node.getFirstChild().getNodeValue();
                            } else if (node.getNodeName().equals("Director")) {
                                movieDirector = node.getFirstChild().getNodeValue();
                            } else if (node.getNodeName().equals("Actor")) {
                                movieActor = node.getFirstChild().getNodeValue();
                            } else if (node.getNodeName().equals("Romance")) {
                                movieRomance = node.getFirstChild().getNodeValue();
                            }else if (node.getNodeName().equals("Type")) {
                                movieType = node.getFirstChild().getNodeValue();
                            }
                            else if (node.getNodeName().equals("Price")) {
                                moviePrice = node.getFirstChild().getNodeValue();
                            }
                            else if (node.getNodeName().equals("Schedule")) {
                                NodeList items = node.getChildNodes();
                                for (int z = 0; z < items.getLength(); z++) {
                                    Node it = items.item(z);
                                    //System.out.println();
                                    if (it.getNodeName().equals("Item")) {
                                        item = it.getFirstChild().getNodeValue();
                                        System.out.println("电影名称\t"+"\t"+"英文名称\t "+"\t"+"导演"+"\t"+"演员"+"\t"+"电影类型\t"+"\t"+"价格"+"\t"+"时间" + "\n"
                                        + movieName+"\t" + moviePoster+"\t"
                                                + movieDirector+"\t" + movieActor+"\t" + movieRomance+"\t" +movieType+"\t"+ moviePrice+"\t" + item );
                                    }
                                }
                            }
                        }
                    }
                }
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
            } catch (SAXException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
